﻿using System;
using Bookvar.ObjectModel;
using Bookvar.ObjectModel.Actions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Bookvar.UnitTests.ObjectModel.Actions
{
    /// <summary>
    /// Tests for the <see cref="AddFloatingTopicActionTests"/> class.
    /// </summary>
    [TestClass]
    public class AddFloatingTopicActionTests : CommonActionTests
    {
        /// <summary>
        /// Creates the <see cref="AddFloatingTopicActionTests"/> to be tested for common action behavior.
        /// </summary>
        /// <returns></returns>
        protected override IAction CreateAction()
        {
            return new AddFloatingTopicAction( new Topic(), new MindMap() );
        }

        [TestMethod]
        public void CanCreateNew()
        {
            var action = new AddFloatingTopicAction( new Topic(), new MindMap() );
            Assert.IsNotNull( action );
        }

        [TestMethod]
        [ExpectedException( typeof ( ArgumentNullException ) )]
        public void New_WithNullTopic_Throws()
        {
            new AddFloatingTopicAction( null, new MindMap() );
        }

        [TestMethod]
        [ExpectedException( typeof ( ArgumentNullException ) )]
        public void New_WithNullMindMap_Throws()
        {
            new AddFloatingTopicAction( new Topic(), null );
        }

        [TestMethod]
        [ExpectedException( typeof ( InvalidOperationException ) )]
        public void Execute_WithNoTopic_Throws()
        {
            new AddFloatingTopicAction { Mindmap = new MindMap() }.Execute();
        }

        [TestMethod]
        [ExpectedException( typeof ( InvalidOperationException ) )]
        public void Execute_WithNoMindMap_Throws()
        {
            new AddFloatingTopicAction { Topic = new Topic() }.Execute();
        }


        [TestMethod]
        public void Execute_AddsTheTopicAsFloatingToTheMindMap()
        {
            var mindMap = new MindMap();
            var topic = new Topic();

            new AddFloatingTopicAction( topic, mindMap ).Execute();

            Assert.AreEqual( 1, mindMap.FloatingTopics.Count );
            CollectionAssert.Contains( mindMap.FloatingTopics, topic );
        }

        [TestMethod]
        public void Undo_RemovesTheTopicFromTheMindMapFloatingTopics()
        {
            var mindMap = new MindMap();
            var topic = new Topic();

            var action = new AddFloatingTopicAction( topic, mindMap );
            action.Execute();
            action.Undo();

            Assert.AreEqual( 0, mindMap.FloatingTopics.Count );
        }
    }
}